package com.sunkai.collection;

import java.util.Collection;




public class ArrayList<T> {

	private int size;
	
	private int capacity = 10;
	
	private Object[] value = new Object[capacity];
	
	public boolean add(T t) {
		verifyCapacity(1);
//		value[size] = t;
//		size++;
		value[size++] = t;
		return true;
	}
	
	public boolean add(Collection<T> collection) throws Exception {
		if (collection == null) {
			throw new Exception("collection can not be null");
		}
		verifyCapacity(collection.size());
		for (T ele : collection) {
			value[size++] = ele;
		}
		return true;
	}
	
	private void verifyCapacity(int length) {
		if (capacity - size < length) {
			//kuo
			capacity = (length/capacity + 2)* capacity;
			Object[] newValue = new Object[capacity];
			System.arraycopy(value, 0, newValue, 0, size);
			value = newValue;
		}
	}
	
	
	public T get(int index) throws Exception {
		if (index >= size || index < 0) {
			throw new Exception("******************ArrayIndexOutOfBoundsException******************");
		}
		return (T) value[index];
	}
	
	public String toString() {
		if (size == 0) {
			return "[]";
		}
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		for (int i = 0; i < capacity; i++) {
			sb.append(value[i]).append(",");
		}
		sb.substring(0, sb.length() - 1);
		sb.append("]");
 		return sb.toString();
	}
	
	public T remove(int index) throws Exception {
		if (index >= size || index < 0) {
			throw new Exception("******************ArrayIndexOutOfBoundsException******************");
		}
		@SuppressWarnings("unchecked")
		T t = (T) value[index];
		if (index == (size -1)) {
			value[index] = null;
		} else {
			System.arraycopy(value, index + 1, value, index, size - index - 1);
			value[size-1] = null;
		}
		size--;
		return t;
	}
	
	public int indexOf(Object o) {
		if (o == null) {
			for (int i = 0; i < size; i++) {
				if (value[i] == null)
					return i;
			}
		} else {
			for (int i = 0; i < size; i++) {
				if (o.equals(value[i])) {
					return i;
				}
			}
		}
		return -1;
	}
	
	public int lastIndexOf(Object o) {
		if (o == null) {
			for (int i = size -1; i >= 0; i--) {
				if (value[i] == null) {
					return i;
				}
			}
		} else {
			for (int i = size -1; i >=0; i--) {
				if (o.equals(value[i])) {
					return i;
				}
			}
		}
		return -1;
	}
	
	
	public static void main(String[] args) throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i <= 10; i++) {
			list.add(i+"");
		}
		System.out.println(list);
		list.remove(9);
		System.out.println(list);
		list.add(999+"");
		System.out.println(list);
		System.out.println(arrayList.lastIndexOf("c"));
		
		list.add(arrayList);
		System.out.println(list);
		
		java.util.ArrayList list2 = new java.util.ArrayList();
	}
	public static java.util.ArrayList<String> arrayList = new java.util.ArrayList<String>();
	static {
		arrayList.add("a");
		arrayList.add("b");
		arrayList.add("c");
		arrayList.add("d");
		arrayList.add("a");
		arrayList.add("b");
	}
}
